import { createSlice } from "@reduxjs/toolkit"
// import axios from "axios"
import { billList, addBills } from "@/apis"

const billStore = createSlice({
  name: 'bill',
  // 数据状态state
  initialState: {
    billList: []
  },
  // 同步修改方法
  reducers: {
    setBillList (state, action) {
      state.billList = action.payload
    },
    addBill (state, action) {
      state.billList.push(action.payload)
    }
  }
})

// 解构
const { setBillList, addBill } = billStore.actions

// 编写异步请求
const getBillList = () => {
  return async (dispatch) => {
    const res = await billList()
    dispatch(setBillList(res))
  }
}
const addBillList = (params) => {
  return async (dispatch) => {
    const res = await addBills(params)
    dispatch(addBill(res))
  }
}

// 获取reducer
const reducer = billStore.reducer

export { getBillList, addBillList }
export default reducer